package edu.just.pet.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.druid.sql.visitor.functions.Char;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import edu.just.pet.entity.User;
import edu.just.pet.service.UserService;
import edu.just.pet.system.Constants;
import org.omg.IOP.Encoding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login.do")
    @ResponseBody
    public Map login(User user, HttpSession session){
        System.out.println(user);
        System.out.println("logining:"+user);
        Map<String, Object> result = new HashMap<>();

        // 用户名密码不能为空
        if(StrUtil.isEmpty(user.getUsername()) || StringUtil.isEmpty(user.getPassword())) {
            result.put(Constants.RETURN_CODE, Constants.CODE_FAIL);
            result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
            return result;
        }

        // 调用service，去查找用户是否存在
        User userInDb = userService.check(user);

        // 比较加密后的密码是否相等
//        String encrypPassword = SecureUtil.md5(user.getPassword());
//        System.out.println(encrypPassword);

        System.out.println("userInDb"+userInDb);
        if(userInDb != null && userInDb.getPassword().equals(user.getPassword())) {
            session.setAttribute("userLoginName",userInDb.getUsername());
            session.setAttribute("userLoginId",userInDb.getId());
            session.setAttribute("status","user");
            result.put(Constants.RETURN_DATA,userInDb);
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
        }

        return result;
    }
    @RequestMapping("logout.do")
    @ResponseBody
    public Map logout(HttpSession session){
        Map<String,Object> result=new HashMap<>();
        session.invalidate();
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
        return result;
    }
    @RequestMapping("afterLogin.do")
    @ResponseBody
    public ModelAndView afterLogin(User user){
        Map<String, Object> result = new HashMap<>();
        System.out.println(user);
        User userInDb=userService.getUserInfoById(user);
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
        result.put(Constants.RETURN_DATA,userInDb);

        return new ModelAndView("/front/userhomepage",result);
    }

    @RequestMapping("index.do")
    public ModelAndView index() {
        Map<String, Object> result = new HashMap<>();

        List<User> userList = userService.getUserListByPage(new User());

        result.put(Constants.RETURN_DATA, userList);
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, "请求成功");

        return new ModelAndView("/back/users/user_index", result);
    }

    /**
     * 获得用户列表
     * @param user
     * @return
     */
    @RequestMapping("getUserList.do")
    @ResponseBody
    public Map index(User user){

        Map<String,Object> result =new HashMap<>();

        List<User> userList=userService.getUserListByPage(user);
        PageInfo<User> pageInfo=new PageInfo<>(userList);

        result.put(Constants.RETURN_DATA, pageInfo.getList());
        result.put(Constants.RETURN_COUNT, pageInfo.getTotal());
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, "请求成功");

        return result;
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("deleteUser.do")
    @ResponseBody
    public Map<String, Object> deleteUser(User user) {
        Map<String, Object> result = new HashMap<>();

        if(userService.deleteUser(user) > 0) {
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, "删除成功");
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, "删除失败");
        }
        return result;
    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @RequestMapping("deleteUserList.do")
    @ResponseBody
    public Map<String, Object> deleteUserList(@RequestBody List<User> userList) {
        Map<String, Object> result = new HashMap<>();

        if(userService.deleteUserList(userList) > 0) {
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, "删除成功");
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, "删除失败");
        }
        return result;
    }

    @RequestMapping("toAdd.do")
    public ModelAndView toAdd(User user) {

        Map<String, Object> result = new HashMap<>();

        // 调用service层
        result.put(Constants.RETURN_CODE, "1");
        result.put(Constants.RETURN_MSG, "请求成功");

        return new ModelAndView("/back/users/user_add", result);
    }

    /**
     * 添加
     * @param
     * @return
     */
    @RequestMapping("addUser.do")
    @ResponseBody
    public Map<String, Object> addUser(User user) {

        Map<String, Object> result = new HashMap<>();

        System.out.println(user);
        // 校验参数
        if(StrUtil.isEmpty(user.getUsername()) || user.getPassword() == null) {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.PARAM_ERROR);

            return result;
        }
        try {
            // 业务层写在try catch块中

            // 加密密码
//            String encrypPassword = SecureUtil.md5(user.getPassword());
//            user.setPassword(encrypPassword);

            userService.addUser(user);
            User userInDB=userService.check(user);
            result.put(Constants.RETURN_DATA,userInDB);
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
        }
        return result;
    }

    @RequestMapping("toUpdate.do")
    @ResponseBody
    public ModelAndView toUpdate(User user){
        Map<String, Object> result = new HashMap<>();

        // 调用service层
        User user1=userService.getUserInfoById(user);

        result.put(Constants.RETURN_DATA, user1);
        result.put(Constants.RETURN_CODE, "1");
        result.put(Constants.RETURN_MSG, "请求成功");

        return new ModelAndView("/back/users/user_update", result);
    }

    @RequestMapping("updateUser.do")
    @ResponseBody
    public Map<String, Object> updateUser(User user) {
        System.out.println(user);
        Map<String, Object> result = new HashMap<>();
        // 校验参数
        if(StrUtil.isEmpty(user.getUsername()) || user.getPassword() == null) {

            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.PARAM_ERROR);
            System.out.println("null");
            return result;
        }

        if(userService.updateUser(user)>0) {
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, "修改成功");
            System.out.println("success");
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, "修改失败");
            System.out.println("failed");
        }
        return result;
    }

}
